import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    mobile: "",
    bankName: "",
    bankCardNumber: "",
    userId: "",
    userName: "",
    amount: 0,
    date: "",
    show: false,
    startTime: "",
    endTime: "",
    minDate: new Date(2021, 0, 1).getTime(),
    allChecked: false,
    maxDate: new Date(2025, 0, 1).getTime(),
    showPriceConfirm: false,
    selectedOrderList: [],
    totalNum: 0,
    orderNumber: 0,
    orderList: [],
    packList: []
  },
  getData() {
    const { userId, startTime, endTime, companyId } = this.data;
    let s = startTime ? wx.$dayjs(startTime).format("YYYY-MM-DD") + " 00:00:00" : "";
    let e = endTime ? wx.$dayjs(endTime).format("YYYY-MM-DD") + " 23:59:59" : "";
    wx.$apis
      .getSettlementSalaryPage({
        userId,
        companyId,
        // startTime: s,
        // endTime: e,
        asc: true,
        columns: [],
        current: 1,
        currentMongo: 0,
        size: 500
      })
      .then((res) => {
        if (res.data) {
          const { records } = res.data;
          this.setData({
            orderList: records
          });
        }
      });
  },
  onDisplay() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      startTime: this.formatDate(start),
      endTime: this.formatDate(end)
    });
    this.getData();
  },
  formatDate(date = new Date()) {
    return wx.$dayjs(date).format("YYYY/MM/DD");
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const companyId = app.globalData.userInfo.companyData.companyId;
    let userId = options.id;
    let userName = options.name;
    let mobile = options.mobile;
    this.setData({
      companyId,
      userId,
      userName,
      mobile
    });
    this.getData();
  },

  onChange: function (event) {
    let index = event.currentTarget.dataset.index;
    let orderList = this.data.orderList;
    orderList[index].checked = !orderList[index].checked;
    let allChecked = true;
    if (orderList && orderList.length) {
      orderList.forEach((item) => {
        if (!item.checked) {
          allChecked = false;
        }
      });
    } else {
      allChecked = false;
    }
    let selectedOrderList = orderList.filter((item) => item.checked);
    let totalNum = 0;
    selectedOrderList.forEach((item) => {
      totalNum += item.count * 1;
    });
    this.setData({
      totalNum,
      orderList,
      selectedOrderList,
      allChecked
    });
  },
  checkAll: function (event) {
    let orderList = this.data.orderList;
    let totalNum = 0;
    if (event.detail) {
      orderList.forEach((item) => {
        totalNum += item.count * 1;
        item.checked = true;
      });
    } else {
      orderList.forEach((item) => {
        item.checked = false;
      });
    }
    let selectedOrderList = orderList.filter((item) => item.checked);
    this.setData({
      totalNum,
      orderList: orderList,
      selectedOrderList: selectedOrderList,
      allChecked: event.detail
    });
  },
  onPay() {
    let selectedOrderList = this.data.selectedOrderList;
    if (selectedOrderList && selectedOrderList.length) {
      this.setData({
        showPriceConfirm: true,
        selectedOrderList
      });
      this.getPackCounts();
    } else {
      Toast.fail("请勾选订单");
    }
  },
  // 结账前获取结算数据
  getPackCounts() {
    const { allChecked, companyId, amount, endTime, startTime, userId, selectedOrderList } = this.data;
    let s = startTime ? wx.$dayjs(startTime).format("YYYY-MM-DD") + " 00:00:00" : "";
    let e = endTime ? wx.$dayjs(endTime).format("YYYY-MM-DD") + " 23:59:59" : "";
    let params = {
      userId,
      startTime: s,
      endTime: e,
      allIn: allChecked,
      companyId,
      // amount,
      outOrderIds: selectedOrderList.map((item) => {
        return item.id;
      })
    };
    if (allChecked) {
      params.outOrderIds = undefined;
    }
    wx.$apis.getPackCounts(params).then((res) => {
      if (res.data) {
        let orderNumber = 0;
        res.data.forEach((item) => {
          orderNumber += item.packCount * 1;
        });
        this.setData({
          orderNumber,
          packList: res.data
        });
      }
    });
  },
  hideConfirm() {
    this.setData({
      showPriceConfirm: false
    });
  },
  printConfirm() {
    this.hideConfirm();
  },
  submitConfirm() {
    this.submit();
  },
  // 打款
  submit() {
    const { allChecked, companyId, amount, endTime, startTime, userId, selectedOrderList } = this.data;
    let s = startTime ? wx.$dayjs(startTime).format("YYYY-MM-DD") + " 00:00:00" : "";
    let e = endTime ? wx.$dayjs(endTime).format("YYYY-MM-DD") + " 23:59:59" : "";
    let params = {
      userId,
      startTime: s,
      endTime: e,
      allIn: allChecked,
      companyId,
      amount: wx.$getFen(amount),
      outOrderIds: selectedOrderList.map((item) => {
        return item.id;
      })
    };
    wx.showLoading({
      title: "结账中..."
    });
    wx.$apis.getSettlementSalary(params).then((res) => {
      this.hideConfirm();
      wx.showToast({
        title: "结款成功",
        icon: "success",
        duration: 2000
      }).then((r) => {
        setTimeout(() => {
          wx.navigateBack();
        }, 1000);
      });
    });
  },
  // 去选择银行卡
  toSelectBank() {
    let params = {
      ...this.data
    };
    wx.setStorage({
      key: "unsettled",
      data: {
        unsettled: params
      },
      success: (res) => {
        wx.navigateTo({
          url: "/pages/management/select-bank-card-management/index"
        });
      }
    });
  },
  onShow() {
    wx.getStorage({
      key: "unsettled",
      success: (res) => {
        const { unsettled } = res.data;
        this.setData({ ...unsettled });
        wx.removeStorage({
          key: "unsettled"
        });
      }
    });
  }
});
